/*
 * Copyright 2007 MBARI
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.mbari.vcr;

import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import org.mbari.vcr.rs422.VCR;

/**
 * Class description
 *
 *
 * @version    $Id: $
 * @author     Brian Schlining    
 */
public class VCRTestRxtx2 extends VCRTestRxtx1 {

    /**
     * Constructs ...
     *
     *
     * @param serialPortName
     *
     * @throws IOException
     * @throws NoSuchPortException
     * @throws PortInUseException
     * @throws UnsupportedCommOperationException
     */
    public VCRTestRxtx2(String serialPortName)
            throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
        super(serialPortName);

        // TODO Auto-generated constructor stub
    }

    /**
     * Constructs ...
     *
     *
     * @param serialPortName
     * @param receiveTimeout
     *
     * @throws IOException
     * @throws NoSuchPortException
     * @throws PortInUseException
     * @throws UnsupportedCommOperationException
     */
    public VCRTestRxtx2(String serialPortName, long receiveTimeout)
            throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
        super(serialPortName, receiveTimeout);
    }

    void init(String serialPortName)
            throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
        super.init(serialPortName);
        serialPort.notifyOnDataAvailable(true);

        try {
            serialPort.addEventListener(new SerialPortEventListener() {

                public void serialEvent(SerialPortEvent evt) {
                    System.out.println(evt);

                    switch (evt.getEventType()) {

                        case SerialPortEvent.BI :

                        case SerialPortEvent.OE :

                        case SerialPortEvent.FE :

                        case SerialPortEvent.PE :

                        case SerialPortEvent.CD :

                        case SerialPortEvent.CTS :

                        case SerialPortEvent.DSR :

                        case SerialPortEvent.RI :

                        case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
                            break;

                        case SerialPortEvent.DATA_AVAILABLE :
                            byte[] readBuffer = new byte[20];

                            try {
                                while (in.available() > 0) {
                                    int numBytes = in.read(readBuffer);
                                    System.out.print(new String(readBuffer));
                                }


                            }
                            catch (IOException e) {}

                            break;
                    }

                }

            });
        }
        catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * Method description
     *
     *
     * @param args
     */
    public static void main(String[] args) {
        int ok = 0;
        try {
            VCRTestRxtx1 test = new VCRTestRxtx1("COM2", 500);
            test.send(VCR.GET_STATUS);
            test.send(VCR.GET_TIMECODE);
            test.send(VCR.PLAY_FWD);
            test.send(VCR.GET_STATUS);
            test.send(VCR.GET_TIMECODE);
            Thread.sleep(2000);
            test.send(VCR.STOP);
            test.send(VCR.GET_TIMECODE);
            Thread.sleep(2000);
            test.send(VCR.GET_TIMECODE1);
            Thread.sleep(2000);
            test.send(VCR.GET_TIMECODE2);
            Thread.sleep(2000);
            test.send(VCR.GET_STATUS);
            Thread.sleep(2000);
        }
        catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
            ok = -1;
        }

        System.exit(ok);
    }
}
